package com.yc.rh.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.yc.rh.utils.SessionAttributeNames;
import com.yc.rh.biz.INewartInfoBiz;
import com.yc.rh.entities.JsonObject;
import com.yc.rh.entities.NewartInfo;

public class NewartInfoAction extends ActionSupport implements ServletRequestAware{
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;
	private INewartInfoBiz newartInfoBiz;
	private NewartInfo newartInfo;
	private JsonObject<NewartInfo> jsonObject;
	private HttpSession session;

	private File file;
	private List<File> myfile;
	private String fileFileName;
	private List<String> myfileFileName;
	private String fileFileContentType;
	private List<String> myfileFileContentType;
	private InputStream inputStream;

	private String message = "你已成功上传文件";

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public List<File> getMyfile() {
		return myfile;
	}

	public void setMyfile(List<File> myfile) {
		this.myfile = myfile;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getMyfileFileName() {
		return myfileFileName;
	}

	public void setMyfileFileName(List<String> myfileFileName) {
		this.myfileFileName = myfileFileName;
	}

	public String getFileFileContentType() {
		return fileFileContentType;
	}

	public void setFileFileContentType(String fileFileContentType) {
		this.fileFileContentType = fileFileContentType;
	}

	public List<String> getMyfileFileContentType() {
		return myfileFileContentType;
	}

	public void setMyfileFileContentType(List<String> myfileFileContentType) {
		this.myfileFileContentType = myfileFileContentType;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
		this.session=request.getSession();
	}

	public INewartInfoBiz getNewartInfoBiz() {
		return newartInfoBiz;
	}

	public void setNewartInfoBiz(INewartInfoBiz newartInfoBiz) {
		this.newartInfoBiz = newartInfoBiz;
	}

	public NewartInfo getNewartInfo() {
		return newartInfo;
	}

	public void setNewartInfo(NewartInfo newartInfo) {
		this.newartInfo = newartInfo;
	}

	public JsonObject<NewartInfo> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JsonObject<NewartInfo> jsonObject) {
		this.jsonObject = jsonObject;
	}

	public String getNewartByxx(){
		String value=request.getParameter("value");
		String name=request.getParameter("name");
		NewartInfo ni=new NewartInfo();
		int total=0;
		if(value!=null){
			if("natime".equals(name)){
				ni.setNatime(value);
			}else if("naname".equals(name)){
				ni.setNaname(value);
			}
			total=newartInfoBiz.getTotal(ni);
			System.out.println(total+"   "+ni);
		}else{
			total=newartInfoBiz.getTotal();
		}
		session.setAttribute("value", value);
		session.setAttribute("name", name);

		jsonObject.setTotal(total);
		return "success";
	}

	public String getPicInfoDate() {
		List<NewartInfo> list=newartInfoBiz.find(1,8);
		jsonObject.setRows(list);
		return "success";
	}

	/**
	 * 页面加载显示
	 * @param request
	 * @param response
	 */
	public String getPicInfo() {
		int pageNo;
		if(request.getParameter("pageNo")==null){
			pageNo=1;
		}else{
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}

		int pageSize=12;	//默认每页显示12条

		int totalSize=newartInfoBiz.getTotal();  //总记录数
		int totalPage=(totalSize+pageSize-1)/pageSize;  //总页数
		List<NewartInfo> list=newartInfoBiz.find(pageNo,pageSize);
		jsonObject.setRows(list);
		jsonObject.setTotalPage(totalPage);
		jsonObject.setPageNo(pageNo);
		jsonObject.setTotalSize(totalSize);
		return "success";
	}

	/**
	 * 图片的加载
	 * @param request
	 * @param response
	 */
	public String indexDatas() {
		int naid= (Integer) session.getAttribute(SessionAttributeNames.COMMENTDATA);
		String naContentImg=newartInfoBiz.findNaContentImgByNaid(naid);
		jsonObject.setContent(naContentImg);
		//session.setAttribute(SessionAttributeNames.COMMENTDATA, null);
		//session.removeAttribute(SessionAttributeNames.COMMENTDATA);
		return "success";
	}

	/**
	 * 修改作品信息
	 * @param request
	 * @param response
	 */
	public String updateNewartInfo() {
		int naid=Integer.parseInt(request.getParameter("naid"));
		String naContentImg=(String) session.getAttribute(SessionAttributeNames.CURRENTPIC);
		String naname=request.getParameter("naname");
		NewartInfo ni=new NewartInfo();
		ni.setNaid(naid);
		ni.setNaContentImg(naContentImg);
		ni.setNaname(naname);

		session.setAttribute(SessionAttributeNames.CURRENTPIC, null);
		session.removeAttribute(SessionAttributeNames.CURRENTPIC);
		jsonObject.setResult(newartInfoBiz.updatePic(ni));
		return "success";
	}

	/**
	 * 修改图片
	 * @param request
	 * @param response
	 */
	public String updateNewartPic() {
		String src=request.getParameter("src");
		int naid=Integer.parseInt(request.getParameter("naid"));
		String naContentImg;
		if(session.getAttribute(SessionAttributeNames.CURRENTPIC)==null){
			naContentImg=newartInfoBiz.findNaContentImgByNaid(naid);
		}else{
			naContentImg=(String) session.getAttribute(SessionAttributeNames.CURRENTPIC);
		}
		if(naContentImg.contains(",")){
			if(naContentImg.contains(src+",")){
				naContentImg=naContentImg.replace(src+",","");
			}else{
				naContentImg=naContentImg.replace(","+src,"");
			}
		}else{
			naContentImg=naContentImg.replace(src,"暂无");
		}

		session.setAttribute(SessionAttributeNames.CURRENTPIC, naContentImg);
		jsonObject.setResult(1);
		return "success";
	}

	/**
	 * 添加照片入相册
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("resource")
	public String addNewartPic() {
		String path = ServletActionContext.getServletContext().getRealPath("/pics");
		String ss="RHouse\\";
		path=path.replace(ss, "");
		int size=0;
		try {
			System.out.println(this.getMyfile()+" ddd "+this.getFile());
			List<File> f = this.getMyfile();
			size=f.size();
			System.out.println(this.getFileFileName()+" ddd "+myfileFileName);
			for(int i=0;i<size;i++){
			
			if(myfileFileName.get(i).endsWith(".exe")){
				message="对不起,你上传的文件格式不允许!!!";
				return ERROR;
			}
			FileInputStream inputStream = new FileInputStream(f.get(i));
			FileOutputStream outputStream = new FileOutputStream(path + "/"+ myfileFileName.get(i));
			byte[] buf = new byte[1024];
			int length = 0;
			while ((length = inputStream.read(buf)) != -1) {
				outputStream.write(buf, 0, length);
			}
			
			inputStream.close();
			outputStream.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = "对不起,文件上传失败了!!!!";
		}
		
		String naContentImg=newartInfoBiz.findNaContentImgByNaid(newartInfo.getNaid());
		System.out.println(size);
		for(int i=0;i<size;i++){
		if("null".equals(naContentImg) || "暂无".equals(naContentImg)){
			naContentImg="../pics/"+ myfileFileName.get(i);
		}else{
			naContentImg=naContentImg+",../pics/"+ myfileFileName.get(i);
		}
		System.out.println(naContentImg);
		}
		newartInfo.setNaContentImg(naContentImg);
		jsonObject.setResult(newartInfoBiz.updatePic(newartInfo));
		return "success";
	}

	/**
	 * 获取作品名
	 * @param request
	 * @param response
	 */
	public String getAllNewartInfo() {
		List<NewartInfo> list=newartInfoBiz.find(null, null);
		jsonObject.setRows(list);
		return "success";
	}

	/**
	 * 添加作品
	 * @param request
	 * @param response
	 */
	@SuppressWarnings("resource")
	public String addNewartInfo() {
		String path = ServletActionContext.getServletContext().getRealPath("/pics");
		String ss="RHouse\\";
		path=path.replace(ss, "");
		try {
			System.out.println(this.getMyfile()+" ddd "+this.getFile());
			File f = this.getFile();
			System.out.println(this.getFileFileName());
			if(this.getFileFileName().endsWith(".exe")){
				message="对不起,你上传的文件格式不允许!!!";
				return ERROR;
			}
			FileInputStream inputStream = new FileInputStream(f);
			FileOutputStream outputStream = new FileOutputStream(path + "/"+ this.getFileFileName());
			byte[] buf = new byte[1024];
			int length = 0;
			while ((length = inputStream.read(buf)) != -1) {
				outputStream.write(buf, 0, length);
			}
			inputStream.close();
			outputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
			message = "对不起,文件上传失败了!!!!";
		}

		newartInfo.setNapic("../pics/"+ this.getFileFileName());
		jsonObject.setResult(newartInfoBiz.addNewart(newartInfo));
		return "success";
	}

	/**
	 * 删除作品
	 * @param request
	 * @param response
	 */
	public String delNewartInfo() {
		String naids=request.getParameter("naids");
		jsonObject.setResult(newartInfoBiz.delNews(naids));
		return "success";
	}

	/**
	 * 查询所有作品
	 * @param request
	 * @param response
	 */
	public String getNewart() {
		if(request.getParameter("page")!=null){
			int page=Integer.parseInt(request.getParameter("page"));
			int rows=Integer.parseInt(request.getParameter("rows"));
			int total=0;
			List<NewartInfo> list;
			if(session.getAttribute("name")==null){
				list=newartInfoBiz.find(page, rows);
				total=newartInfoBiz.getTotal();
			}else{
				NewartInfo ni=new NewartInfo();
				String name=(String) session.getAttribute("name");
				String value=(String) session.getAttribute("value");
				if(value!=null){
					if("natime".equals(name)){
						ni.setNatime(value);
					}else if("naname".equals(name)){
						ni.setNaname(value);
					}
					list=newartInfoBiz.findNewartInfoByDate(ni, page, rows);
					total=newartInfoBiz.getTotal(ni);
				}else{
					list=newartInfoBiz.find(page, rows);
					total=newartInfoBiz.getTotal();
				}
				session.setAttribute("name", null);
				session.removeAttribute("name");
				session.setAttribute("value", null);
				session.removeAttribute("value");
			}
			jsonObject.setRows(list);
			jsonObject.setTotal(total);
		}
		return "success";
	}
}
